package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.Staff;
import com.wanshi.service.IStaffService;
import com.wanshi.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息表 前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-16
 */
@RestController
@CrossOrigin
@RequestMapping("/staff")
@Api(tags = "测试StaffController接口文档")
public class StaffController {

    @Autowired
    private IStaffService staffService;


    @ApiOperation(value = "员工带条件分页")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="ye", value="页数", paramType = "path",required = true),
                    @ApiImplicitParam(name="size", value="一页显示多少条", paramType = "path", required = true)
            }
    )
    @PostMapping("/findPageAndCountSta/{ye}/{size}")
    public Result findPageAndCountSta(@PathVariable int ye, @PathVariable int size, @RequestBody Staff staff){
        Page page = new Page(ye,size);
        QueryWrapper<Staff> wrapper = new QueryWrapper();
        if(staff!=null){
            if(!StringUtils.isEmpty(staff.getName())){
                wrapper.like("name", staff.getName());
            }
            if(!StringUtils.isEmpty(staff.getUsername())){
                wrapper.like("username", staff.getUsername());
            }
        }
        wrapper.orderByDesc("id");
        staffService.page(page,wrapper);
        return Result.ok(page);
    }

    @ApiOperation(value = "添加员工")
    @PostMapping
    public Result addStaff(@RequestBody Staff staff) {
        return staffService.add(staff);
    }

    @ApiOperation(value = "根据id删除员工")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteStaff(@PathVariable String id){
        boolean b = staffService.removeById(id);
        if(b){
            return Result.ok();
        }
        return Result.error("删除员工失败");
    }

    @ApiOperation(value = "修改员工")
    @PutMapping ("UpdateStaff")
    public Result UpdateStaff(@RequestBody Staff staff) {
        boolean b = staffService.updateById(staff);
        if(b){
            return Result.ok();
        }
        return Result.error("修改员工失败");
    }


    @ApiOperation(value = "根据id查找员工")
    @GetMapping("/{id}")
    public Result get(@PathVariable String id){
        Staff staff = staffService.getById(id);
        return Result.ok(staff);
    }

    @ApiOperation(value = "修改或者添加员工")
    @PostMapping ("/staff")
    public Result addAndUpdate(@RequestBody Staff staff) {
        boolean b = staffService.saveOrUpdate(staff);
        if(b){
            return Result.ok();
        }
        return Result.error("修改员工失败");
    }
}

